﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO;
using System.Windows.Media.Imaging;
using ria = HelfenHelfen.RIA.Web.Models;

namespace HelfenHelfen.RIA.Views
{
    public partial class DemandDetails : Page
    {
        ria.Image _image = null;
        ria.Demand _demand = null;

        public DemandDetails()
        {
            InitializeComponent();

            this.demandDomainDataSource.SubmittedChanges += new EventHandler<SubmittedChangesEventArgs>(demandDomainDataSource_SubmittedChanges);
            this.demandDomainDataSource.LoadedData +=new EventHandler<LoadedDataEventArgs>(demandDomainDataSource_LoadedData);
        }

        void demandDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            MessageBox.Show("Changes saved!");
            NavigationService.Navigate(new Uri("/DemandList", UriKind.Relative));
        }

        private void demandDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var d = Guid.Parse(NavigationContext.QueryString["ID"]);
            this.demandDomainDataSource.QueryParameters.Add(
                new Parameter()
                {
                    ParameterName = "ID",
                    Value = d
                });
        }
            
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.demandDomainDataSource.SubmitChanges();
        }

        private void ImageBorder_Drop(object sender,
            DragEventArgs e)
        {
            if (e.Data != null &&
                e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(files[0].OpenRead());

                this.WebcamImage.Source = bmp;

                
            }
        }

        CaptureSource _captureSource = null;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _captureSource = new CaptureSource();
            _captureSource.VideoCaptureDevice
                = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

            if (CaptureDeviceConfiguration.AllowedDeviceAccess
                || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                _captureSource.Start();

                VideoBrush brush = new VideoBrush();
                brush.SetSource(_captureSource);

                this.WebcamImage.Source = null;
                this.ImageBorder.Background = brush;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (_captureSource != null)
            {
                _captureSource.CaptureImageCompleted += new EventHandler<CaptureImageCompletedEventArgs>(_captureSource_CaptureImageCompleted);
                _captureSource.CaptureImageAsync();
            }
        }

        void _captureSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
        {
            if (e.Cancelled == false && e.Error == null)
            {
                this.WebcamImage.Source = e.Result;
                _captureSource.Stop();
            }
        }

    }
}
